library(foreign)
electionDat <- read.dta("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/matchingReplication.dta")

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Cexp <- Cexp/sd(Cexp,na.rm=T)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
Iexp <- Iexp/sd(Iexp,na.rm=T)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=as.character(electionDat$yrStDist),stringsAsFactors=F))

dat$dist <- substr(dat$yrStDist,5,nchar(dat$yrStDist))

library(lme4)
est <- glm(Cwin~CNtriv+Cqual+Ivs+Iexp+Cexp+(Rinc+Dinc)*as.factor(year),data=dat,family=binomial,x=T)

X <- est$x
X1 <- X
X0 <- X
X1[,2] <- 1
X0[,2] <- 0 

beta <- coef(est)
lp1 <- X1%*%cbind(beta)
lp0 <- X0%*%cbind(beta)

pr0 <- plogis(lp0)
pr1 <- plogis(lp1)

library(xtable)
estRes <- summary(est)$coef[,1:3]

xtable(est)

pdf(file="~/Dropbox/professional/Research/Submitted/Camp/Tex/logitEff.pdf",family="Times",height=4,width=4,pointsize=12)
par(mar=c(4,4,.1,.1),las=1)
plot(pr0,pr1,xlim=c(0,1),ylim=c(0,1),pch=1,col=rgb(0,0,0,alpha=50,maxColorValue=255),ylab="Pr(Challenger Wins| EPN Integration)",xlab="")
title(xlab="Pr(Challenger Wins| No EPN Integration)",line=2)
abline(0,1)
dev.off()




